package com.zeedev.flyingsaucer.beers.activities.storechooser;

import android.os.AsyncTask;
import android.widget.Toast;
import com.google.inject.Inject;
import com.zeedev.flyingsaucer.modules.StoreModule.impl.StoreDatabaseModel;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;

import java.util.Map;
import java.util.Observable;
import java.util.Observer;

public class StoreChooserController implements Observer {

    public static final String TAG = "StoreChooserController";

    @Inject
    StoreChooser mContext;

    @Inject
    UfoClubClient beerWebLink;

    private final StoreDatabaseModel model;

    @Inject
    public StoreChooserController(StoreDatabaseModel model) {
        this.model = model;
        async = new DownloadStoreList();

//		model.addObserver(this);

        refresh(false);

        //TODO should this be working here? --> yes or the screen won't be correct without a refresh
//		this.updateGui();

    }

    private DownloadStoreList async;
    private Boolean isRefreshing = false;

    // params, progress, result
    private class DownloadStoreList extends
            AsyncTask<Void, Void, Map<String, String>> {

        @Override
        protected void onPostExecute(final Map<String, String> result) {

            isRefreshing = false;
            if (result != null) {
                //TODO ??? FIX
//				model.setStoreUrlModel(result);
            } else {
                Toast.makeText(mContext, "There was an error getting the store list.", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        protected void onPreExecute() {
            isRefreshing = true;
            updateGui();
        }

        @Override
        protected Map<String, String> doInBackground(Void... params) {
            Map<String, String> res = null;

            try {
                res = beerWebLink.getStoresURLs();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return res;
        }
    }

    private void updateGui() {
//		Log.d(TAG,"update gui.");
        StoreDatabaseModel scm = this.model;
        if (!scm.isEmpty()) {
            mContext.setSpinnerModel(scm);
            mContext.setDataReady(true);
        } else {
            mContext.setDataReady(false);
        }

        mContext.setIndicatorVisible(isRefreshing, false);


    }

    @Override
    public void update(Observable observable, Object data) {
//		Log.d(TAG,"Update fired from model.");
        this.updateGui();
    }

    public StoreDatabaseModel getModel() {
        return model;
    }

    public void refresh(boolean always) {
//		Log.d(TAG,"refresh requested.");
        if (always) {
            switch (async.getStatus()) {
                case FINISHED:
                    async = new DownloadStoreList();
                    async.execute();
                    break;
                case PENDING:
                    async.execute();
                    break;
                case RUNNING:
                    Toast.makeText(mContext, "You have already started a refresh.", Toast.LENGTH_SHORT).show();
                    break;
            }
        } else {
            if (model.isEmpty()) {
                switch (async.getStatus()) {
                    case FINISHED:
                        async = new DownloadStoreList();
                        async.execute();
                        break;
                    case PENDING:
                        async.execute();
                        break;
                    case RUNNING:
                        Toast.makeText(mContext, "A refresh has already been started.", Toast.LENGTH_SHORT).show();
                        break;
                }
            } else {
                //model has data.... do nothing??? ie no refresh
            }

        }


    }


}
